import { Injectable } from '@nestjs/common';
import { CreateCommentDto } from './dto/create-comment.dto';
import { UpdateCommentDto } from './dto/update-comment.dto';
import { PrismaService } from 'src/prisma/prisma.service';

@Injectable()
export class CommentService {
  constructor(private readonly prismaService: PrismaService) {}
  async create(createCommentDto: CreateCommentDto, userId: number) {
    const res = await this.prismaService.comment.create({
      data: {
        userId: +userId,
        postId: +createCommentDto.postId,
        content: createCommentDto.content,
        parentId: +createCommentDto.parentId || null,
      },
    });

    return res;
  }

  findCommnetByPost(id: number) {
    const res = this.prismaService.comment.findMany({
      where: {
        postId: id,
        parentId: null,
      },
      include: {
        user: {
          select: {
            id: true,
            username: true,
            avatar: true,
          },
        },

        replies: {
          include: {
            user: {
              select: {
                id: true,
                username: true,
                avatar: true,
              },
            },
          },
        },
      },
    });

    return res;
  }

  update(id: number, updateCommentDto: UpdateCommentDto) {
    return `This action updates a #${id} comment`;
  }

  remove(id: number) {
    return `This action removes a #${id} comment`;
  }
}
